import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import {Location} from '@angular/common';

import { HeroService } from '../hero.service';

import { Hero } from '../hero.model';

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class HeroesComponent implements OnInit {
  heroes: Hero[];
  selectedHero: Hero;
  constructor(
    private location: Location,
    private heroService: HeroService,
  ) { }

  ngOnInit() {
    this.getHeroes();
  }

  getHeroes(): void {
    this.heroService.getHeroes()
      .subscribe(heroes => this.heroes = heroes);
  }

  goBack(): void {
    this.location.back();
  }

  onSelect(hero): void {
    this.selectedHero = hero;
  }
}
